﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using NSsh.Common.Types;
using NUnit.Framework.SyntaxHelpers;
using System.IO;
using System.Collections;

namespace NSsh.Common.Tests.Types
{
    [TestFixture]
    public class NameListFixture
    {
        [Test]
        public void ShouldCreateEmpty()
        {
            NameList list = new NameList();

            Assert.That(list.Names.Count, Is.EqualTo(0), "Name list count incorrect.");
            Assert.That(list.ToByteArray().Length, Is.EqualTo(4), "Name list data incorrect.");
        }

        [Test]
        public void ShouldCreateFromStringList()
        {
            string[] names = { "name1", "name2", "name3" };
            NameList list = new NameList(new List<string>(names));

            Assert.That(list.Names, Is.EquivalentTo(new List<string>(names)), "Name list data incorrect.");
            Assert.That(list.ToByteArray().Length, Is.EqualTo(4 + 5 * 3 + 1 * 2), "Name list length incorrect.");
        }

        [Test]
        public void ShouldFailIfNameContainsComma()
        {
            Assert.That(new MethodInvoker(delegate
            {
                string[] names = { "name1", "nam,e2", "name3" };
                NameList list = new NameList(new List<string>(names));
                list.ToByteArray();

            }), Throws.Exception<ArgumentException>());
        }

        [Test]
        public void ShouldCreateFromBinaryReader()
        {
            String names = "name1,name2,name3";
            string[] namesArray = { "name1", "name2", "name3" };
            ICollection namesList = new List<string>(namesArray);

            MemoryStream memoryStream = new MemoryStream();

            BinaryWriter writer = new BinaryWriter(memoryStream);
            writer.WriteBE(5 * 3 + 1 * 2);
            writer.Write(Encoding.ASCII.GetBytes(names));

            memoryStream.Seek(0, SeekOrigin.Begin);

            BinaryReader reader = new BinaryReader(memoryStream);

            NameList list = new NameList(reader);

            Assert.That(list.Names, Is.EquivalentTo(namesList), "Name list data incorrect.");
            Assert.That(list.ToByteArray(), Is.EqualTo(memoryStream.ToArray()), "ToByteArray result wrong.");
        }
    }
}
